# Maintainer: Diego Sogari <diego.sogari@falker.com.br>

_realname=liblas
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.8.1
pkgrel=2
pkgdesc="ASPRS LiDAR data translation toolset (mingw-w64)"
arch=('any')
url="https://liblas.org/"
license=('BSD')
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-cmake")
depends=("${MINGW_PACKAGE_PREFIX}-gdal"
         "${MINGW_PACKAGE_PREFIX}-laszip")
source=("${_realname}-${pkgver}.tar.gz::https://codeload.github.com/libLAS/libLAS/tar.gz/${pkgver}"
        "${_realname}-1.8.1.patch"
        "liblas-upstream-fixes.patch")
sha256sums=('31cff4fd235f9397df8c3086b0aefc50a047d2be0e9ce39f0cddfc662633fa93'
            '1a04a04ee064e54a86b229826336d34239ec6e178c23b67b57bba01933df8592'
            'cbcb261f4bf4b2f9f2c82810f66791ebcb4f11008333becf7de7219c57ca521b')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -Np1 -i "${srcdir}/${_realname}-1.8.1.patch"
  patch -Np1 -i "${srcdir}/liblas-upstream-fixes.patch"
}

build() {
  declare -a _btype
  if check_option "debug" "y"; then
    _btype=Debug
  else
    _btype=Release
  fi

  rm -rf "${srcdir}/build-${MINGW_CHOST}"
  mkdir "${srcdir}/build-${MINGW_CHOST}"
  cd "${srcdir}/build-${MINGW_CHOST}"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_BUILD_TYPE=${_btype} \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DBUILD_OSGEO4W=ON \
    -DWITH_LASZIP=ON \
    "../${_realname}-${pkgver}"

  make
}

check() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  export GDAL_DATA="${MINGW_PREFIX}/share/gdal"
  make test
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make DESTDIR="${pkgdir}" install
  
  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for _f in "${pkgdir}${MINGW_PREFIX}"/lib/cmake/libLAS/*.cmake; do
    sed -e "s|${PREFIX_WIN}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
  done
}
